home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: Philippe Verdy <100105.3120@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Forward declarations: why won't they work?
- Date: 23 Mar 1996 22:00:48 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4j1sag$qnt@arl-news-svc-3.compuserve.com>
- NNTP-Posting-Host: ad53-232.compuserve.com
-
- gusty@clark.net (Harlan Messinger) s'Θcrit :
- > Theodore Sternberg (strnbrg@rahul.net) wrote:
- > : Forward class declarations only seem to work sometimes. When they don't
- > : work, I get a compiler error to the effect that "struct foo is an
- > : imcomplete type". Can anyone tell me what's going on, i.e. when forward
- > : class declarations are and are not possible?
- > :
- >
- > You'll get an error when the compiler would have to know, not just that
- > your struct is a struct, but what's IN the struct, in order to compile
- > your code.
- >
- > For example,
- >
- > struct Foo;
- > struct Bar
- > {
- > Foo *f;
- > };
- >
- > is fine. The compiler knows that Foo is a struct, and that f is therefore
- > a pointer-to-struct. The way a compiler sets aside space for a
- > pointer-to-struct is independent of the contents of the struct, so the
- > compiler is able to deal with the definition of Bar without having to
- > know what a Foo looks like. But,
- >
- > struct Foo;
- > struct Bar
- > {
- > Foo f;
- > };
- >
- > won't work because now you're telling the compiler that a Foo itself, not
- > a pointer to it, is a member of Bar, and you are asking the computer to
- > set aside space for a Foo itself. To do this, the compiler _does_ have to
- > know what a Foo looks like. Since you haven't told it yet, you will get an
- > error.
-
- I should add also the following code which does not work
- either:
- struct Foo:
- struct Bar {
- Foo *f;
- }
-
- int FooBar(Bar *b) {
- (b->f)++; // error here
- }
- because b->f is a pointer to an unknown structure, which then
- has no arithmetic defined on it (the size of Foo is unknown).
-